/*eslint-disable */
export default function (app) {

  app.directive('authorizationView', authorizationView);

  function authorizationView($rootScope) {
    'ngInject';

    return {
      multiElement: true,
      restrict: 'A',
      link: linkFn,
    };

    function linkFn(scope, elem, attrs, ctrl) {
      let toUrl = attrs.authorizationView;

      scope.$watch('authorizationInfo', function (val) {
        if (val && val.menuList && !isAuthenticatedUrl(toUrl, val.menuList)) {
          elem.remove();
        }
      });

      function isAuthenticatedUrl(toUrl, menuList) {
        for(let i=0; i<menuList.length; i++){
          let authenticatedUrl = menuList[i].url;
          if (authenticatedUrl && authenticatedUrl.indexOf(toUrl) != -1) {
            return true;
          }
        }
        return false;
      }
    }
  }
}